package com.herench.bs.handler;

import com.herench.bs.transfer.Dialect;
import com.herench.bs.agent.IOutpatientClinicAgent;
import com.herench.bs.annotation.Handler;
import com.herench.bs.support.AbstractMessageHandler;
import com.herench.bs.support.RequestMessage;
import com.herench.bs.support.ResponseMessage;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.Map;

/**
 * Created by ThinkPad on 2017/9/7.
 */

@Component
@Handler(code = "100710",name="预约挂号")
public class AppointmentHandler extends AbstractMessageHandler {
//
//    @Value("${userList}")
//    private String userList;
    @Override
    protected ResponseMessage innerHandler(RequestMessage requestMessage) throws Exception {
        ResponseMessage responseMessage = new ResponseMessage(requestMessage.getId());
        String channelCode = requestMessage.getArgsPropertyStringValue("sysCode");
        Assert.notNull(channelCode, "sysCode 为空");
        String doctorCode = requestMessage.getArgsPropertyStringValue("docId");

        String deptCode = requestMessage.getArgsPropertyStringValue("deptId");
        Assert.notNull(deptCode, "deptCode 为空");

        String schDate = requestMessage.getArgsPropertyStringValue("schDate");
        Assert.notNull(schDate, "schDate 为空");

        String ampm = requestMessage.getArgsPropertyStringValue("ampm");
        Assert.notNull(ampm, "ampm 为空");
        String timeDesc = Dialect.getAmpmValue(ampm);
        String period = requestMessage.getArgsPropertyStringValue("period");
        Assert.notNull(period, "period 为空");
        String[] arr = period.split("~");
        String startTime = arr[0];
        Assert.notNull(startTime, "startTime 为空");

        String endTime = arr[1];
        Assert.notNull(endTime, "endTime 为空");
        String categor = requestMessage.getArgsPropertyStringValue("categor");

        String cardId = requestMessage.getArgsPropertyStringValue("cardId");

        String idCard = requestMessage.getArgsPropertyStringValue("idCard");

        String user = requestMessage.getArgsPropertyStringValue("user");
//        if (StringUtils.hasLength(userList)){
//            String[] tempArray = userList.split(",");
//            List<String> phoneList = Arrays.asList(tempArray);
//            if (!phoneList.contains(user)){
//                throw new RuntimeException("服务暂未开发，敬请期待!");
//            }
//        }
        Assert.notNull(user, "user 为空");
        String regFee = requestMessage.getArgsPropertyStringValue("regFee");
        String hospitalCode = getCleverClinicContext().getHospitalCodeByChannelCode(channelCode);
        IOutpatientClinicAgent agent = getCleverClinicContext().getApplicationContext().getBean(IOutpatientClinicAgent.class);
        Map<String, Object> res = agent.doAppoint(channelCode, hospitalCode, doctorCode, deptCode, schDate, timeDesc, regFee, "0", "0", "0", user, cardId, "0", idCard, startTime, endTime, "D");
        getPropertiesFromInvokerResultMap(responseMessage, res);
        responseMessage.setData(res);

        return responseMessage;
    }
}
